import COS from './cos-js-sdk-v5.min.js'

const Bucket = 'aa-1257564155';
const Region = 'ap-chengdu';

var cos = new COS({
  getAuthorization: function (options, callback) {
    wx.teamApi.account.cos({ httpMethod: 'put' }).then(res => {
      var data = res.data.result.data;
      callback({
        TmpSecretId: data.credentials && data.credentials.tmpSecretId,
        TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
        XCosSecurityToken: data.credentials && data.credentials.sessionToken,
        ExpiredTime: data.expiredTime,
      });
    })
  }
});
// 选择文件
function img(num) {
  return new Promise((resolve, reject) => {
    wx.chooseImage({
      count: num, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认用原图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log('27')
        console.log(res)
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success: (ress) => {
            // console.log(ress)
            // if(ress.width>500 || ress.height>500){
            //   wx.showToast({
            //     title: '请上传500X500以下的头像!',
            //     icon:'none'
            //   })
            //   return;
            // }else{
            //   let imglen = res.tempFilePaths.length
            //   let imgPathList = []
            //   for (let i = 0; i < imglen; i++) {
            //     wx.showLoading({
            //       title: '上传中...',
            //       mask: true,
            //     })
            //     let filepath = res.tempFilePaths[i];
            //     let filename = filepath.substr(filepath.lastIndexOf('/') + 1);
            //     console.log(filename)
            //     cos.postObject({
            //       Bucket: Bucket,
            //       Region: Region,
            //       Key: filename,
            //       FilePath: filepath
            //       // onProgress: function (info) {
            //       //   console.log(JSON.stringify(info));
            //       // }
            //     }, function (err, data) {
            //       err && reject(err)
            //       imgPathList.push(data)
            //       if (imgPathList.length >= imglen) {
            //         wx.hideLoading()
            //         resolve(imgPathList)
            //       }
            //     });
            //   }
            // }

            let imglen = res.tempFilePaths.length
            let imgPathList = []
            for (let i = 0; i < imglen; i++) {
              wx.showLoading({
                title: '上传中...',
                mask: true,
              })
              let filepath = res.tempFilePaths[i];
              let filename = filepath.substr(filepath.lastIndexOf('/') + 1);
              console.log(filename)
              cos.postObject({
                Bucket: Bucket,
                Region: Region,
                Key: filename,
                FilePath: filepath
                // onProgress: function (info) {
                //   console.log(JSON.stringify(info));
                // }
              }, function (err, data) {
                err && reject(err)
                imgPathList.push(data)
                if (imgPathList.length >= imglen) {
                  wx.hideLoading()
                  resolve(imgPathList)
                }
              });
            }

          }
        })
      }
    });
  })
}



function uploadImg(res) {
  return new Promise((resolve, reject) => {
    let imglen = res.length
    let imgPathList = []
    for (let i = 0; i < imglen; i++) {
      wx.showLoading({
        title: '上传中...',
        mask: true,
      })
      let filepath = res[i];
      let filename = filepath.substr(filepath.lastIndexOf('/') + 1);
      cos.postObject({
        Bucket: Bucket,
        Region: Region,
        Key: filename,
        FilePath: filepath
        // onProgress: function (info) {
        //   console.log(JSON.stringify(info));
        // }
      }, function (err, data) {
        err && reject(err)
        // imgPathList.push(data)
        imgPathList[i] = data
        let tempArray = []
        imgPathList.forEach((item) => {
          if (item) {
            tempArray.push(item)
          }
        })
        if (tempArray.length >= imglen) {
          wx.hideLoading()
          resolve(imgPathList)
        }
      });
    }
  })
}
function video() {
  return new Promise((resolve, reject) => {
    wx.chooseVideo({
      compressed: false,
      success: (res) => {
        console.log(res)
        let filepath = res.tempFilePath;
        let filename = filepath.substr(filepath.lastIndexOf('/') + 1);
        wx.showLoading({
          title: '上传中...',
          mask: true,
        })
        cos.postObject({
          Bucket: Bucket,
          Region: Region,
          Key: filename,
          FilePath: filepath,
          onProgress: function (info) {
            console.log(JSON.stringify(info));
          }
        }, function (err, data) {
          wx.hideLoading()
          err && reject(err)
          data && resolve(data)
        });
      }
    })
  })
}
function uploadVideo(res, cover) {
  return new Promise((resolve, reject) => {
    console.log(res)
    let filepath = res;
    let covername = cover.substr(cover.lastIndexOf('/') + 1);
    let filename = filepath.substr(filepath.lastIndexOf('/') + 1);
    let count = 0
    let video = {}
    wx.showLoading({
      title: '上传中...',
      mask: true,
    })
    cos.postObject({
      Bucket: Bucket,
      Region: Region,
      Key: filename,
      FilePath: filepath,
      onProgress: function (info) {
        console.log(JSON.stringify(info));
      }
    }, function (err, data) {
      err && reject(err)
      if (data) {
        count++
        video.src = data.Location
      }
      if (count === 1) {
        wx.hideLoading()
        resolve(video)
      }
    })
    // cos.postObject({
    //   Bucket: Bucket,
    //   Region: Region,
    //   Key: covername,
    //   FilePath: cover,
    //   onProgress: function (info) {
    //     console.log(JSON.stringify(info));
    //   }
    // }, function (err, data) {
    //   err && reject(err)
    //   if (data) {
    //     count++
    //     video.cover = data.Location
    //   }
    //   if (count === 2) {
    //     wx.hideLoading()
    //     resolve(video)
    //   }
    // });
  })
}
// 初始化实例
module.exports = {
  img: img,
  uploadImg: uploadImg,
  video: video,
  uploadVideo: uploadVideo
}